package com.yylany.controller;

import com.yylany.bo.Specification;
import com.yylany.bo.User;
import com.yylany.enums.ResultEnum;
import com.yylany.mapper.TbSpecificationMapper;
import com.yylany.pojo.TbUser;
import com.yylany.service.UserService;
import com.yylany.utils.Verification;
import com.yylany.vo.BaseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Description: 用户的前台接口
 *
 * @author <a href="mailto:17777502615@163.com">yaoyonglong</a>
 * @date 2018/12/17
 * @since 1.0.0
 */
@RestController
@RequestMapping
public class UserController {
    private UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    /**
     * 注册账号，需要提供账号密码，手机号，以及验证码
     *
     * @param user
     * @return
     */
    @PostMapping("/regist")
    public BaseResult regist(@RequestBody User user) {


        // 创建返回对象
        BaseResult br = null;
        /**
         * 需要校验用户传过来的值
         */
        if (Verification.verifyMobile(user.getMobile()) &&
                StringUtils.isNotEmpty(user.getName()) &&
                StringUtils.isNotEmpty(user.getPassword()) &&
                StringUtils.isNotEmpty(user.getCode())) {
            /**
             * 假如插入成功，不会出现错误，假如出现错误，表示该账户以及存在
             */
            try {
                userService.saveUser(user);
                br = new BaseResult(ResultEnum.REGIST_SUCCESS.getErrno(), ResultEnum.REGIST_SUCCESS.getErrmsg());
            } catch (DuplicateKeyException e) {
                br = new BaseResult(ResultEnum.REGIST_MSG.getErrno(), ResultEnum.REGIST_MSG.getErrmsg());
            }
            // 校验失败
        } else {
            br = new BaseResult(ResultEnum.REGIST_MSG.getErrno(), ResultEnum.REGIST_MSG.getErrmsg());
        }
        return br;
    }

    /**
     * 根据手机号码发送注册短信，
     *
     * @param user 需要校验手机号码，
     * @return
     */
    @PostMapping("/sms")
    public BaseResult sms(@RequestBody TbUser user) {
        BaseResult br = null;
        //需要判断用户输入的手机号码是否符合规则，如果不符合则报错
        if (!Verification.verifyMobile(user.getMobile())) {
            br = new BaseResult(ResultEnum.MOBILE_MSG.getErrno(), ResultEnum.MOBILE_MSG.getErrmsg());
        } else {
            br = new BaseResult(ResultEnum.MOBILE_SUCCESS.getErrno(), ResultEnum.MOBILE_SUCCESS.getErrmsg());
        }
        return br;
    }


    @GetMapping("/query")
    public ResponseEntity<Object> queryUser(
            @RequestParam("mobile") String mobile,
            @RequestParam("password") String password
    ) {
        // 根据mobile查找用户信息
        TbUser user = userService.findUserByMobile(mobile);
        if (user == null || !user.getPassword().equals(password)) {
            return ResponseEntity.ok(null);
        }
        return ResponseEntity.ok(user);
    }

    @Autowired
    TbSpecificationMapper tbBrandMapper;

    @GetMapping("/user/{id}")
    public List<Specification> getAll(@PathVariable(name = "id") int id) {
        return tbBrandMapper.findAllByCategoryId(id);
    }

}
